package com.taokatao.cardmee.business.carddetail;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taokatao.cardmee.R;
import com.taokatao.cardmee.WebActivity;
import com.taokatao.cardmee.api.model.CardDetailInfo;
import com.taokatao.cardmee.api.security.ServiceUrlProvider;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardDetailCourseLayout extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.t_course_title) TextView tCourseTitle;
    @BindView(R.id.t_course_desc) TextView tCourseDesc;
    @BindView(R.id.i_course_url) ImageView iCourseUrl;

    @BindView(R.id.l_content) View lContent;

    private CardDetailInfo.CourseOrdered mCourseOrderd;
    private boolean hasDetailUrlLink;

    public CardDetailCourseLayout(Context context) {
        this(context, null);
    }

    public CardDetailCourseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_detail_course, this));
    }

    public void setData(CardDetailInfo.CourseOrdered courseOrderd, boolean hasDetailUrlLink) {
        if (courseOrderd == null) return;
        mCourseOrderd = courseOrderd;
        this.hasDetailUrlLink = hasDetailUrlLink;
        tCourseTitle.setText(mCourseOrderd.getDescString());

        lContent.setOnClickListener(this);

        if (hasDetailUrlLink) {
            iCourseUrl.setVisibility(View.VISIBLE);
            lContent.setBackgroundResource(R.drawable.selector_item_white);
        } else {
            iCourseUrl.setVisibility(View.GONE);
            lContent.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == lContent) {
            if (hasDetailUrlLink) {
                Intent intent = new Intent(v.getContext(), WebActivity.class);
                intent.putExtra("extra_url", ServiceUrlProvider.obtainOrderedCourseDetailsUrl(mCourseOrderd.courseId , 1));
                v.getContext().startActivity(intent);
            }
        }
    }
}
